import { app, BrowserWindow, ipcMain, shell } from 'electron'
import path from 'path'
import { electronApp, is, optimizer } from '@electron-toolkit/utils'
import { registerLyricEvent2IpcMain } from '../renderer/src/ipcMain/lyric'
let win: BrowserWindow
function createWindow(): void {
  // Create the browser window.
  win = new BrowserWindow({
    width: 1022,
    height: 670,
    show: false,
    frame: false,
    minWidth: 1022,
    minHeight: 670,
    backgroundColor: '#fff',
    title: '网易云音乐',
    icon: 'src/renderer/wyyyy.ico',
    autoHideMenuBar: true,
    ...(process.platform === 'linux'
      ? {
        icon: path.join(__dirname, '../../build/icon.png')
      }
      : {}),
    webPreferences: {
      preload: path.join(__dirname, '../preload/index.js'),
      sandbox: false,
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false
    }
  })

  win.on('ready-to-show', () => {
    win.show()
  })

  win.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  win.on('ready-to-show', () => {
    win.show()
  })
  ipcMain.on('window-close', () => {
    app.exit()
  })
  ipcMain.on('window-max', () => {
    if (win.isMaximized()) { // true表示窗口已最大化.
      win.restore()
      return
    }
    win.maximize()
  })
  ipcMain.on('window-min', () => {
    win.minimize()
  })
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    win.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    win.loadFile(path.join(__dirname, '../renderer/index.html'))
  }
}
// 歌词窗口
let lyricWindow: BrowserWindow
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
function createLyricWindow() {
  let parent_size_x = win.getSize()[0]
  lyricWindow = new BrowserWindow({
    webPreferences: {
      preload: path.join(__dirname, '../preload/index.js'),
      nodeIntegration: true,
      sandbox: false,
      contextIsolation: false,
      webSecurity: false
    },
    height: 130,
    width: parent_size_x * 0.60,
    // 透明背景
    // transparent: true,
    // 置顶
    alwaysOnTop: true,
    // 无边框
    frame: false,
    show: false,
    title: '网易云音乐歌词栏'
  })

  // 使用穿透窗口
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    // win.loadURL(process.env['ELECTRON_RENDERER_URL'])
    lyricWindow.loadURL(process.env['ELECTRON_RENDERER_URL'] + '/#/lyric')
  } else {
    lyricWindow.loadURL('app:///#/lyric')
  }
  // lyricWindow.hide()
  // 调整位置
  const parent_x = win.getPosition()[0]
  const parent_size_y = win.getSize()[1]
  const settings_size_x = lyricWindow.getSize()[0]
  const settings_size_y = lyricWindow.getSize()[1]
  const settings_new_x = parent_x + (parent_size_x - settings_size_x) / 2
  const settings_new_y = (parent_size_y - settings_size_y) / 6
  lyricWindow.setPosition(settings_new_x, settings_new_y, false)
}

// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron')
  // Default open or close DevTools by F12 in development
  // and ignore CommandOrControl + R in production.
  // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  createWindow()
  createLyricWindow()
  registerLyricEvent2IpcMain(lyricWindow)

  app.on('activate', function() {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
